﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using System.IO;

using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class goods_type :BaseAdmin
    {


        // Methods
        private void AddPicture(string imgpath, string str)
        {
            SQLAccess.DoSql("insert into DB_Shuxintype(shuxiname,languages,pic) values('" + str.ToString() + "'," + info.languages("") + ",'" + imgpath + "')");
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            StringBuilder builder = new StringBuilder("");
            string[] strArray = base.Request.Form["text"].Split(new char[] { ',' });
            for (int i = 0; i < files.Count; i++)
            {
                if (files[i].FileName.Length > 0)
                {
                    HttpPostedFile file = files[i];
                    if ((file.ContentLength / 0x400) > 0x400)
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---不能大于1024k<br>");
                        break;
                    }
                    string extension = Path.GetExtension(file.FileName);
                    if (((extension != ".jpg") && (extension != ".JPG")) && ((extension != ".gif") && (extension != ".GIF")))
                    {
                        builder.Append(Path.GetFileName(file.FileName) + "---图片格式不对，只能是jpg或gif<br>");
                        break;
                    }
                }
            }
            if (builder.Length <= 0)
            {
                string str2 = "pic002";
                string path = base.Server.MapPath("../../uploadfile/flash") + @"\" + str2;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                Random random = new Random();
                int num2 = 1;
                this.upcontent();
                for (int j = 0; j < files.Count; j++)
                {
                    HttpPostedFile file2 = files[j];
                    string fileName = "";
                    string str5 = "";
                    string imgpath = "";
                    fileName = Path.GetFileName(file2.FileName);
                    string str7 = random.Next(100, 0x5f5e100).ToString() + num2.ToString();
                    string str8 = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + str7;
                    if (fileName.Length > 0)
                    {
                        str5 = Path.GetExtension(file2.FileName);
                        string filename = path + @"\" + str8 + str5;
                        file2.SaveAs(filename);
                        imgpath = @"flash\" + str2 + @"\" + str8 + str5;
                    }
                    if (str5.ToString() != "")
                    {
                        this.AddPicture(imgpath, strArray[j]);
                    }
                    num2++;
                }
                base.Response.Write("<script language='javascript'>alert('更新成功');window.location.href='goods_type.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.ProductDefaultParament);
                this.upbind();
            }
        }

        private void upbind()
        {
            DataSet set = SQLAccess.GreatDs("select * from DB_Shuxintype where languages=" + info.languages("") + "");
            this.DataList1.DataSource = set;
            this.DataList1.DataBind();
            if ((base.Request.QueryString["sid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("delete from DB_Shuxintype where shuxinid=" + base.Request.QueryString["sid"].ToString() + "");
                }
                base.Response.Write("<script language='javascript'>alert('删除成功');window.location.href='goods_type.aspx';</script>");
                base.Response.End();
            }
        }

        private void upcontent()
        {
            string sql = "select * from DB_Shuxintype order by shuxinid asc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    SQLAccess.DoSql("update [DB_Shuxintype] set [shuxiname]='" + base.Request.Form["content_" + row["shuxinid"].ToString()] + "' where shuxinid=" + row["shuxinid"].ToString() + "");
                }
            }
        }

  

    }
}